/* Created by Aquariuslt on 5/20/17.*/


FoodsController.$inject = ['foodsService', '$scope', '$location','$http','$uibModal'];
function FoodsController(foodsService, $scope, $location,$http,$uibModal) {
  var $ = require('jquery');
  $('#nav').show();
  var vm = $scope.vm = {};
  console.log(sessionStorage.merchant);
 vm.merchant = JSON.parse(sessionStorage.merchant);
  foodsService.foodlist(vm.merchant.icnum, function (err, foodlist) {
    vm.foodlist = foodlist;
    console.log(vm.foodlist);
  });



  vm.addAd = function (foodid) {
    console.log("addAd " + foodid);
    var f = {};
    vm.foodlist.forEach(item=>{
      if(item._id==foodid){
        f=item;
      }
    });
    var data = {};
    data.merchant = vm.merchant;
    data.food = f;
      var request = {
                method: 'POST',
                url: '/m/foodAd/createFoodAdByMerchant/',
                data:data
            };

            return $http(request).then(function(res, status, headers) {
              alert("推荐成功");
            });
  }

  vm.upFood = function (foodid) {
    console.log("upFood " + foodid);
    $location.path('/upFood/' + foodid);

  }

  vm.deleteFood = function (foodid) {
    console.log("deleteFood " + foodid);
    foodsService.deleteFood(foodid, function (err, food) {
      let flag = -1;
      let index = -1;
      vm.foodlist.forEach(item => {
        index++;
        if (item._id == foodid) {
          flag = index;
        }
      });
      if (flag != -1)
        vm.foodlist.splice(flag, 1);
      console.log(flag);
    });
  }

  vm.addFood = function () {
    console.log("addFood");
    $location.path('/addFood');
  }
  
        setInterval(reload,2000);
           function reload(){
              var request = {
                method: 'GET',
                url: '/m/merchant/getMerchantStatus2ByIcnum/'+vm.merchant.icnum
            };

            return $http(request).then(function(res, status, headers) {
              console.log(res.data);
              if(res.data==0){
                      console.log("aaa");
                       $("#black").modal("show");
                		}else{
                		   $("#black").modal("hide");
                		}
            });
                	 
           }

}

module.exports = FoodsController;